package com.dtw.ditouwang.ui;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.DrawableRes;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.dtw.ditouwang.R;
import com.dtw.ditouwang.models.WebApi;
import com.dtw.ditouwang.utils.SharedPreferencesUtil;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class BaseActivity extends AppCompatActivity {
    protected WebApi webApi;
    protected SharedPreferencesUtil sharedPreferencesUtil;
    private AlertDialog alertDialog;
    public BaseActivity (){
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient.Builder okHttpClient=new OkHttpClient.Builder();
        okHttpClient.addInterceptor(httpLoggingInterceptor);

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(WebApi.BaseUrl)
//                .addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .client(okHttpClient.build())
                .build();
        webApi = retrofit.create(WebApi.class);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//竖屏
        sharedPreferencesUtil = SharedPreferencesUtil.getInstance(this);
        alertDialog=new AlertDialog.Builder(this,R.style.Dialog).setView(new ProgressBar(this)).create();
        alertDialog.setCanceledOnTouchOutside(false);
        alertDialog.getWindow().setDimAmount(0);
    }

    protected void showProgress(){
        if(!alertDialog.isShowing()){
            alertDialog.show();
        }
    }

    protected void closeProgress(){
        if(alertDialog.isShowing()){
            alertDialog.dismiss();
        }
    }

    @Override
    public void setTitle(CharSequence title) {
        ((TextView)findViewById(R.id.text_title)).setText(title);
        ((TextView)findViewById(R.id.text_title)).setSelected(true);
    }

    public String getTitleText(){
        return ((TextView)findViewById(R.id.text_title)).getText().toString();
    }

    protected void setSubTitle(View view, View.OnClickListener onClickListener) {
        ((FrameLayout) findViewById(R.id.text_sub_title)).addView(view);
        findViewById(R.id.text_sub_title).setOnClickListener(onClickListener);
    }

    protected void setSubTitle(@DrawableRes int id, View.OnClickListener onClickListener) {
        ImageView imageView = new ImageView(this);
        imageView.setImageResource(id);
        setSubTitle(imageView, onClickListener);
    }

    protected void setSubTitle(String subTitle, View.OnClickListener onClickListener) {
        TextView textView = new TextView(this);
        textView.setText(subTitle);
        setSubTitle(textView, onClickListener);
    }

    public void enableBack(){
        findViewById(R.id.image_back).setVisibility(View.VISIBLE);
        findViewById(R.id.image_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    public void showToast(String msg){
        Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
    }
}
